package com.business.controller;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.business.domain.CarBrand;
import com.business.domain.CarModel;
import com.business.domain.CarModelVo;
import com.business.domain.CarRefProvince;
import com.business.domain.CarSeries;
import com.business.domain.CarSeriesVo;
import com.business.service.CarService;
import com.sys.Utils.DbConstants;
import com.sys.domain.DictItem;
import com.sys.result.PageParam;
import com.sys.result.RequestPageVo;
import com.sys.result.ResultVo;


@Controller
@RequestMapping("/business/car")
public class CarController {
	
	@Resource
	private CarService carService;
	
	/**
	 * 品牌页
	 */
	@RequestMapping("/carPage")
	public String toBrandPage(Model model, @RequestParam("title") String title,String type){
		model.addAttribute("title", title);
		String str;
		if("brand".equals(type)){
			str = "car/brand";
		}else if("series".equals(type)){
			//获取所有品牌
			List<CarBrand> brandList = carService.getAllCarBrand();
			model.addAttribute("brandList", brandList);
			str = "car/series";
		}else{
			//获取所有品牌  和基础数据
			List<CarBrand> brandList = carService.getAllCarBrand();
			model.addAttribute("brandList", brandList);
			List<DictItem> che_jiegou = carService.getItemByCode(DbConstants.CHE_JIEGOU);
			List<DictItem> jin_qi = carService.getItemByCode(DbConstants.JIN_QI);
			List<DictItem> qu_dong = carService.getItemByCode(DbConstants.QU_DONG);
			List<DictItem> bian_su = carService.getItemByCode(DbConstants.BIAN_SU);
			model.addAttribute("che_jiegou", che_jiegou);
			model.addAttribute("jin_qi", jin_qi);
			model.addAttribute("qu_dong", qu_dong);
			model.addAttribute("bian_su", bian_su);
			str = "car/model";
		}
		return str;
	}
	
	/**
	 * 根据条件分页查询品牌
	 *
	 */
	@RequestMapping("/getBrandList")
	public @ResponseBody RequestPageVo<CarBrand> getBrandList(PageParam param, CarBrand brand){

		return carService.getBrandList(param, brand);
	}
	
	/**
	 * 添加或修改 品牌
	 *
	 */
	@RequestMapping("/saveOrUpdateBrand")
	public @ResponseBody ResultVo saveOrUpdateBrand(CarBrand brand,MultipartFile contractFile){
		return carService.saveOrUpdateBrand(brand,contractFile);
	}
	
	/**
	 * 根据条件分页查询车系
	 */
	@RequestMapping("/getSeriesList")
	public @ResponseBody RequestPageVo<CarSeriesVo> getSeriesList(PageParam param,CarSeries series){
		
		return carService.getSeriesList(param, series);
	}
	/**
	 * 根据条件分页查询车系
	 *
	 */
	@RequestMapping("/getSeriesListByBrandId")
	public @ResponseBody RequestPageVo<CarSeriesVo> getSeriesListByBrandId(Integer brandId){
		
		return carService.getSeriesListByBrandId(brandId);
	}
	
	/**
	 * 添加或修改 车系
	 *
	 */
	@RequestMapping("/saveOrUpdateSeries")
	public @ResponseBody ResultVo saveOrUpdateSeries(CarSeries series){
		return carService.saveOrUpdateSeries(series);
	}
	/**
	 * 根据条件分页查询型
	 */
	@RequestMapping("/getModelList")
	public @ResponseBody RequestPageVo<CarModelVo> getModelList(PageParam param,CarModel model,Integer brandId){
		
		return carService.getModelList(param, model,brandId);
	}
	
	/**
	 * 添加或修改 车型
	 *
	 */
	@RequestMapping("/saveOrUpdateModel")
	public @ResponseBody ResultVo saveOrUpdateModel(CarModel model){
		return carService.saveOrUpdateModel(model);
	}
	
	
	@RequestMapping("/deleteById")
	public @ResponseBody ResultVo deleteById(Integer id,String type){
		return carService.deleteById(id,type);
	}
	@RequestMapping("/deleteReaData")
	public @ResponseBody ResultVo deleteReaData(Integer id){
		return carService.deleteReaData(id);
	}
	@RequestMapping("/backById")
	public @ResponseBody ResultVo backById(Integer id){
		return carService.backById(id);
	}
	
	/**
	 * 添加车型价格
	 */
	@RequestMapping("/addPrice")
	public @ResponseBody ResultVo addPrice(Integer carModelId, Double price, String address){
		return carService.addPrice(carModelId, price, address);
	}
	
	/**
	 * 根据车型id查询该车型的价格
	 */
	@RequestMapping("queryPriceByModelId")
	public @ResponseBody RequestPageVo<CarRefProvince> queryPriceByModelId(Integer carModelId){
		return carService.queryPriceByModelId(carModelId);
	}
	
	@RequestMapping("/deletePrice")
	public @ResponseBody ResultVo deletePrice(Integer id){
		return carService.deletePrice(id);
	}
	
	/**
	 * 校验车型名字是否重复
	 */
	@RequestMapping("/checkedModelName")
	public @ResponseBody Boolean checkedModelName(Integer id,String name,Integer seriesId){
		return carService.checkedModelName(id,name,seriesId);
	}
	/**
	 * 车型库导出
	 * 
	 */
	@RequestMapping("/exportCar")
	public void exportCar(HttpServletResponse res){
		HSSFWorkbook exportCar = carService.exportCar();
		try {
			res.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode("车型库数据.xls", "UTF-8"));  
			ServletOutputStream out = res.getOutputStream();
			exportCar.write(out);
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
